package com.delivery.www.dao;


import com.delivery.www.util.AnnotationUtils;
import com.delivery.www.util.JdbcUtils;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class BaseDao {


    /**
     * 查询单个
     */
    public static <T> T getForOne(Connection connection, Class<T> clazz, String sql, Object... params) {
        PreparedStatement preparedStatement;
        ResultSet resultSet;
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            Field[] fields = clazz.getDeclaredFields();
            T t = null;
            if (resultSet.next()) {
                t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    for (Field field : fields) {
                        AnnotationUtils.Field annotation = field.getAnnotation(AnnotationUtils.Field.class);
                        String s = annotation.columnName();
                        if (s.equals(columnLabel)) {
                            field.setAccessible(true);
                            field.set(t, resultSet.getObject(s));
                        }
                    }
                }
            }
            JdbcUtils.closeResource(null, preparedStatement, resultSet);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询多个
     */
    public static <T> List<T> getList(Connection connection, Class<T> clazz, String sql, Object... params) {
        List<T> list = new ArrayList<>();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            Field[] fields = clazz.getDeclaredFields();
            while (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    for (Field field : fields) {
                        AnnotationUtils.Field annotation = field.getAnnotation(AnnotationUtils.Field.class);
                        String s = annotation.columnName();
                        if (s.equals(columnLabel)) {
                            field.setAccessible(true);
                            field.set(t, resultSet.getObject(s));
                        }
                    }
                }
                list.add(t);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JdbcUtils.closeResource(null, preparedStatement, resultSet);
        }
    }

    /**
     * 增删改
     */
    public static int executeUpdate(Connection connection, String sql, Object... params) {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            JdbcUtils.closeResource(null, preparedStatement, null);
        }
    }

    /**
     * 增加并返回当前索引值
     */
    public static int execute(Connection connection, String sql, Object... params) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet;
        try {
            preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            preparedStatement.executeUpdate();
            resultSet = preparedStatement.getGeneratedKeys();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            } else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            JdbcUtils.closeResource(null, preparedStatement, null);
        }
    }

    /**
     * 查询数量
     */
    public static int selectCount(Connection connection, String sql, Object... params) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet;
        try {
            preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setObject(i + 1, params[i]);
            }
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt("count");
            } else {
                return 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            JdbcUtils.closeResource(null, preparedStatement, null);
        }
    }
}